<?php
namespace Modules\Frontend\Controllers;

class NoticeController extends \Phalcon\Mvc\Controller {
	public function beforeExecuteRoute() {
		\Components\Utils::isClosedFrontent($this);
	}

	public function initialize() {
		$this -> response -> setStatusCode(200, 'OK') -> sendHeaders() -> setContentType('application/json', 'UTF-8');
	}

	public function indexAction() {
		$query = $this -> request -> getQuery();
		if(!isset($query['page'])) {
			$query['page'] = 1;
		} else if($query['page'] <= 0)  {
			$query['page'] = 1;
		} else {
			$query['page'] = abs($query['page']);
		}

		if(!isset($query['limit'])) {
			$query['limit'] = 10;
		} else if($query['limit'] <= 0)  {
			$query['limit'] = 10;
		} else {
			$query['limit'] = abs($query['limit']);
		}

		$service = $this -> notice::getServiceInstance() -> pager([
			'conditions' => 'status = 0',
			'order' => 'notice_id DESC'
		], $query['limit'], $query['page']);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success', 'pager' => $service['pager']]);
	}

	public function homeAction() {
		$service = $this -> notice::getServiceInstance() -> find([
			'columns' => 'content',
			'conditions' => 'status = 0 AND is_home = 1',
			'order' => 'notice_id DESC'
		]);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success','models' => []]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success','models' => $service['models']]);
	}
}